package org.gd136.ce.script.impl;

import java.util.ArrayList;
import java.util.List;

import org.gd136.ce.script.Script;
import org.gd136.ce.script.ScriptStage;

public class SelectScript extends Script {

	public static List<SelectScript> selectStack = new ArrayList<>();

	public static SelectScript getLastSelect() {
		if (selectStack.isEmpty()) {
			return null;
		} else {
			return selectStack.get(selectStack.size() - 1);
		}
	}

	public static void endLastSelect(SelectEndScript ses) {
		SelectScript ss = getLastSelect();
		ss.isHasEnd = true;
		ss.nextScript = ses;
		selectStack.remove(selectStack.size() - 1);
	}

	boolean isHasEnd;
	List<OptionScript> options = new ArrayList<>();

	private static final String TAG = "#select";

	@Override
	public Script readScript(String scriptLine) {
		SelectScript ss = null;
		if (TAG.equals(scriptLine)) {
			ss = new SelectScript();
			ss.addToPoolAndLinkFrontScript();
			selectStack.add(ss);
		}
		return ss;
	}

	@Override
	public void execute(ScriptStage stage) {
		if (!isHasEnd) {
			System.err.println("select 未标记selectEnd! file[" + fileName
					+ "] line[" + lineNum + "]");
		}
		if (!options.isEmpty()) {
			stage.select(options);
		} else {
			System.err.println("select 无option内容! file[" + fileName + "] line["
					+ lineNum + "]");
		}
	}

}
